<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Painel extends MY_Controller {

	public function __construct()
	{
		parent::__construct();
	}

	public function index()
	{	
		redirect('painel/visualizar', 'refresh');
	}

	public function visualizar()
	{	
		$dados = array();

		$this->template->add_js('system/painel/visualizar.js');
		$this->template->write_view('content', 'painel/visualizar', $dados);
		$this->template->render();
// 

	}

	public function agrid_contrato()
	{	

		$limite = $this->input->post('iDisplayLength');
		$start  = $this->input->post('iDisplayStart');
		$sort   = $this->input->post('iSortCol_0');
		$order  = $this->input->post('sSortDir_0');

		switch ($sort) {
			case 0:
				$sort_field = 'c.co_id';
				break;
			case 1:
				$sort_field = 'c.co_nome';
				break;
			break;
			default:
				$sort_field = 'c.co_nome';
				break;
		}

		$this->db
			->select("c.co_id, c.co_nome, c.co_numero_contrato, c.co_doc, c.co_data_entrada, c.co_data_devolucao")
			->from("contrato c")
		;

		$total_registros = $this->db->count_all_results();

		$resp['iTotalRecords']        = $total_registros;
		$resp['iTotalDisplayRecords'] = $total_registros;

		$this->db->order_by("$sort_field $order");
		$this->db->limit($limite, $start);

		$query = $this->db->get();

		$resp['aaData'] = array();
		$indice = 0;
		foreach ($query->result() as $row)
		{
			$botao_data['contrato_id'] = $row->co_id;
			$botao = $this->load->view('painel/botao', $botao_data, TRUE);

			$j = 0;

			$resp['aaData'][$indice][$j++] = $row->co_id;
			$resp['aaData'][$indice][$j++] = $row->co_nome;
			$resp['aaData'][$indice][$j++] = $row->co_numero_contrato;
			$resp['aaData'][$indice][$j++] = $row->co_doc;
			$resp['aaData'][$indice][$j++] = $row->co_data_entrada;
			$resp['aaData'][$indice][$j++] = ($row->co_data_devolucao < date('Y-m-d')) ? '<div class="text-error">'.$row->co_data_devolucao.'</div>' : $row->co_data_devolucao;
			$resp['aaData'][$indice][$j++] = $botao;

			$indice++;
		}

		echo json_encode($resp);	
	}
}